import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/7 10:50
 * @description 单调栈2 栈内存储下标
 */
public class FormalSolution {
    public static void main(String[] args) {

    }
    public int[] nextGreaterElements(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n];
        // 初始化
        Arrays.fill(ans, -1);
        Deque<Integer> stack = new ArrayDeque<>();
        for (int i = 0; i < 2 * n; i++) {
            while (!stack.isEmpty() && nums[stack.peek()] < nums[i % n]) {
                ans[stack.pop()] = nums[i % n];
            }
            // 存储下标
            stack.push(i % n);
        }
        return ans;
    }
}
